/*
 * JANN - a Java toolkit for creating arbitrary Artificial Neural Networks.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.jann.ui;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.Collection;

import javax.swing.Action;

import org.jhotdraw.draw.DelegationSelectionTool;
import org.jhotdraw.draw.Figure;

public class NeuronSelectionTool extends DelegationSelectionTool {

	protected Figure focusFig;
	private static final long serialVersionUID = 2096969028645056438L;
	
	public NeuronSelectionTool() {
		super();
	}
	
	public NeuronSelectionTool(Collection<Action> drawingActions, Collection<Action> selectionActions) {
		super(drawingActions, selectionActions);
	}
	
	@Override
	public void mousePressed( MouseEvent evt ) {
		super.mousePressed(evt);
		Figure f = getDrawing().findFigure(new Point2D.Double(evt.getX(), evt.getY()));
		if ( f != focusFig ) {
			highlightFrom( focusFig, false );
			highlightFrom( f, true );
			focusFig = f;
		}
	}
	
	@Override
	protected void handleMultiClick( MouseEvent evt ) {
		super.handleMultiClick(evt);
		Figure f = getDrawing().findFigureInside( new Point2D.Double(evt.getX(), evt.getY()));
		if ( f != focusFig ) {
			highlightFrom( focusFig, false );
			highlightFrom( f, true );
			focusFig = f;
		}
	}
	
	protected void highlightFrom( Figure focusFig, boolean highlight ) {
		if ( focusFig instanceof NeuronFigure ) {
			NeuronFigure nf = (NeuronFigure) focusFig;
			nf.setHighlighted(highlight);
			for ( LinkFigure f : nf.getIncoming() ) {
				f.setHighlighted(highlight);
				((NeuronFigure) f.getStartFigure()).setHighlighted(highlight);
			}
			for ( LinkFigure f : nf.getOutgoing() ) {
				f.setHighlighted(highlight);
				((NeuronFigure) f.getEndFigure()).setHighlighted(highlight);
			}
		} else if ( focusFig instanceof LinkFigure ) {
			LinkFigure lf = (LinkFigure) focusFig;
			lf.setHighlighted(highlight);
			((NeuronFigure) lf.getStartFigure()).setHighlighted(highlight);
			((NeuronFigure) lf.getEndFigure()).setHighlighted(highlight);
		}
	}
}
